package org.itboys.core.service.mongo.web;

import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.goods.SundayGoodsSizeDto;
import org.itboys.core.entity.mongo.SundayGoodsSize;
import org.itboys.core.util.JNBYRequestUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2018/11/16
 * 描述：尺码
 */
@Service
public class SundayGoodsSizeService extends BaseMongoService<SundayGoodsSize> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayGoodsSize> getEntityClass() {
        return SundayGoodsSize.class;
    }

    public List<SundayGoodsSizeDto> select(String goodsCode) throws IOException {
        List<SundayGoodsSizeDto> sizeDtos = new ArrayList<>();
        JSONObject paramJson = new JSONObject();
        List<String> codeList = new ArrayList<>();
        codeList.add(goodsCode);
        paramJson.put("ids", codeList);
        JSONObject jsonResult = JNBYRequestUtil.request(paramJson, "108d1f62b2e7f5a755230479f5eff13d", SundayCommonConstants.goods_findOne_url);
        List<JSONObject> responseData = (List<JSONObject>) jsonResult.get("data");
        if (ListUtil.isNotNull(responseData)) {
            for (JSONObject object : responseData) {
                List<JSONObject> goodsSizes = (List<JSONObject>) object.get("goodsSize");
                for (JSONObject goodsize : goodsSizes) {
                    SundayGoodsSizeDto sizeDto = new SundayGoodsSizeDto(goodsize.getString("code"), goodsize.getLong("codeId"), goodsize.getLong("goodsId"),
                            goodsize.getLong("kcsl"), goodsize.getString("sizeDetail"), goodsize.getDouble("price"));
                    sizeDtos.add(sizeDto);
                }
            }
        }
        return sizeDtos;
    }

    /**
     * 组装多个
     *
     * @param sizes
     * @return
     */
    public List<SundayGoodsSizeDto> packageMultiDto(List<SundayGoodsSize> sizes) {
        List<SundayGoodsSizeDto> dtos = new ArrayList<>();
        for (SundayGoodsSize size : sizes) {
            SundayGoodsSizeDto dto = new SundayGoodsSizeDto();
            BeanUtils.copyProperties(size, dto);
            dto.setIsSelect(0);
            dtos.add(dto);
        }
        return dtos;
    }

}
